package com.liming.purchaseschema.webapp.wicket.page;


import com.googlecode.wicket.jquery.ui.widget.menu.ContextMenuBehavior;
import com.googlecode.wicket.jquery.ui.widget.menu.IMenuItem;
import com.googlecode.wicket.jquery.ui.widget.menu.MenuItem;
import com.liming.purchaseschema.api.biz.IPurchaseSchemaService;
import com.liming.purchaseschema.core.model.SchemaDefine;
import org.apache.aries.blueprint.annotation.Reference;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import com.googlecode.wicket.jquery.ui.widget.menu.ContextMenu;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * @类描述：
 * @创建人：wangkr
 * @创建时间：2014-04-11 下午1:53
 * @修改人：
 * @修改时间：
 * @修改备注：
 */

public class TreeHaveChildNode extends Panel {
    @Named
    @Inject
    @Reference(id = "purchaseSchemaService", serviceInterface = IPurchaseSchemaService.class)
    private IPurchaseSchemaService purchaseSchemaService;

    public TreeHaveChildNode(String id, SchemaDefine schemaDefine,List schemaDefines,final MyFormPanel formPanel,final TreePanel treePanel){
        super(id);
        final ContextMenu menu = new ContextMenu("menu", getMenuItemList(schemaDefine,treePanel,formPanel)) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onContextMenu(AjaxRequestTarget target, Component component)
            {
                //the menu-item list can be modified here
                //this.getItemList().add(new MenuItem("my new item"));
            }

            @Override
            public void onClick(AjaxRequestTarget target, IMenuItem item)
            {
                this.info("Clicked " + item.getTitle().getObject());

                target.add(this);
            }
        };
        add(menu);

        final Label label1 = new Label("name",schemaDefine.getName());
        label1.add(new ContextMenuBehavior(menu));
        this.add(label1);


        RepeatingView TreeNoChildNodePanel = new RepeatingView("TreeNoChildNodePanel");
        for (int i = 0;i < schemaDefines.size();i++)
        {
            final SchemaDefine schemaDefineSub = (SchemaDefine)schemaDefines.get(i);
            List listSchemaDefine = purchaseSchemaService.findPurchaseSchemaByParent(schemaDefineSub);
            if(listSchemaDefine.size() > 0){
                TreeNoChildNodePanel.add(new TreeHaveChildNode(TreeNoChildNodePanel.newChildId(),schemaDefineSub,listSchemaDefine,formPanel,treePanel));

            }else{
                TreeNoChildNodePanel.add(new TreeNoChildNode(TreeNoChildNodePanel.newChildId(),schemaDefineSub,formPanel,treePanel));
            }
        }
        add(TreeNoChildNodePanel);
        add(label1);
    }

    private List<IMenuItem> getMenuItemList (final SchemaDefine schemaDefine,final TreePanel treePanel,final MyFormPanel formPanel){
        List<IMenuItem> list = new ArrayList<IMenuItem>();

        list.add(new MenuItem("增加节点"){
            @Override
            public void onClick(AjaxRequestTarget target)
            {
                formPanel.AddForm("123s",new SchemaDefine(),schemaDefine,treePanel,formPanel);
                target.add(formPanel);
            }
        });
        list.add(new MenuItem("删除节点"){
            @Override
            public void onClick(AjaxRequestTarget target)
            {
                treePanel.removeTreeNode(schemaDefine);
                treePanel.createTree(formPanel);
                target.add(treePanel);
            }
        });
        list.add(new MenuItem("编辑节点") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target)
            {
                formPanel.StructureForm("123s",schemaDefine,treePanel,formPanel);
                target.add(formPanel);
            }
        });
        return list;
    }
}


